﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HA.ModFlow.Domain.DomainLayer;

namespace HA.Data
{
    [Serializable]
    public class MapDataCollecion
    {
        public MapDataCollecion()
        {
            //this._coverages = new List<CoverageBase>();

            this._coverageCL = new List<Coverage>();

            this._conceptualModelCL = new List<ConceptualModel>();

            _stressPeriods = new StressPeriodCollection();


            StressPeriod sp = new StressPeriod();

            this._stressPeriods.AddStressPeriod(sp);
        }

        bool _isManualSetStressPeriod = false;

        public bool IsManualSetStressPeriod
        {
            get { return _isManualSetStressPeriod; }
            set { _isManualSetStressPeriod = value; }
        }

        GridFrame _gridFrame;

        /// <summary>
        ///网格边框
        /// </summary>
        public GridFrame GridFrame
        {
            get { return _gridFrame; }
           

        }

        StressPeriodCollection _stressPeriods;

        /// <summary>
        /// 应力期对应的设置
        /// </summary>
        public StressPeriodCollection StressPeriods
        {
            get { return _stressPeriods; }

        }



        List<Coverage> _coverageCL;

        public List<Coverage> CoverageCL
        {
            get { return _coverageCL; }
         
        }

        List<ConceptualModel> _conceptualModelCL;

        public List<ConceptualModel> ConceptualModelCL
        {
            get { return _conceptualModelCL; }
            
        }


        //List<CoverageBase> _coverages;

        //public List<CoverageBase> Coverages
        //{
        //    get { return _coverages; }

        //}


        //内部序号分配字段

        int _coverageBase = 1;
        int _conceptualBase = 1;




        [field: NonSerialized]
        public event EventHandler<CollecionChangedEventArgs> ConceputalModelCollectionChanged;

         [field: NonSerialized]
        public event EventHandler<CoverageCollectionChangedEventArgs> CoverageCollecionChanged;

         [field: NonSerialized]
         public event EventHandler<ObjectOperationEventArgs> GridFrameChanged;


         [field: NonSerialized]

         public event EventHandler<FOCollecionChangedEventArgs> FOCollecionChanged;


        public void AskCoverageCollecionChanged(CoverageCollectionChangedEventArgs args)
        {
            if (CoverageCollecionChanged!=null)
	        {
                 CoverageCollecionChanged(this,args);
		 
	        }
        }



        public void AskFOCollecionChanged(FOCollecionChangedEventArgs args)
        {

            if (FOCollecionChanged!=null)
            {
                FOCollecionChanged(this, args);
                
            }
 
        }



        #region 公共函数

        //public void Add(CoverageBase data)
        //{
        //    if (data is Coverage)
        //    {
        //        data.Name = "Coverage" + this._coverageBase.ToString();
        //        //data.Desc = "Coverage" + this._coverageBase.ToString();

        //        this._coverageBase++;
                
                
        //    }

        //    if (data is ConceptualModel)
        //    {
        //        data.Name = "ConcetualModel" + this._conceptualBase.ToString();
        //        //data.Desc = "ConcetualModel" + this._conceptualBase.ToString();
        //        this._conceptualBase++;
                
        //    }

           

        //    this.Coverages.Add(data);
        //    if (this.ConceputalModelCollectionChanged != null)
        //    {
        //        this.ConceputalModelCollectionChanged(this, new CollecionChangedEventArgs(data, CollectionChangedType.ADD));
        //    }
        //}

        //public void Remove(CoverageBase data)
        //{
        //    if (this.Coverages.Contains(data))
        //    {
        //        this.Coverages.Remove(data);
        //        if (this.ConceputalModelCollectionChanged != null)
        //        {
        //            this.ConceputalModelCollectionChanged(this, new CollecionChangedEventArgs(data, CollectionChangedType.REMOVE));
        //        }
        //    }
        //}


        //public void MapTo3DGrid()
        //{
        //    if (this._gridFrame!=null)
        //    {

        //        //this._
                
        //    }
        //}


        public void AddGridFrame(GridFrame gf)
        {
            if (this._gridFrame != null)
            {
                this.EditGridFrame(gf);

            }

            else
            {

                this._gridFrame = gf;

                if (this.GridFrameChanged != null)
                {
                    this.GridFrameChanged(this._gridFrame, new ObjectOperationEventArgs(ObjectOperationType.ADD));

                }

 
            }


          
           
 
        }


        public void RemoveGridFrame()
        {

            if (this._gridFrame!=null)
            {

                this._gridFrame = null;

                if (this.GridFrameChanged != null)
                {
                    this.GridFrameChanged(this._gridFrame, new ObjectOperationEventArgs(ObjectOperationType.REMOVE));

                }

                
            }


         
           
 
        }



        public GridFrame GetAvailableGridFrame()
        {

            if (this._gridFrame != null)
            {

                return this._gridFrame;

            }
            else
            {
 
            }




            return null;

        }


        public void EditGridFrame(GridFrame gf)
        {

            if (this._gridFrame!=gf)
            {

                this._gridFrame = gf;


                if (this.GridFrameChanged != null)
                {
                    this.GridFrameChanged(this._gridFrame, new ObjectOperationEventArgs(ObjectOperationType.MODIFY));

                }
                
            }

         
 
        }


         public void EditGridFrame(double orgX, double orgY, double width, double height, double rotation)
         {
             if (this._gridFrame!=null)
             {
                 this._gridFrame.EditGridFrame(orgX,orgY,width,height,rotation);

                 if (this.GridFrameChanged != null)
                 {
                     this.GridFrameChanged(this._gridFrame, new ObjectOperationEventArgs(ObjectOperationType.MODIFY));

                 }
             }


         }

    

        public ConceptualModel NewConcetualModel()
        {
            ConceptualModel cm = new ConceptualModel();
            cm.Name = "概念模型"+this._conceptualBase.ToString ();
            cm.ParentMapData = this;
            this._conceptualBase += 1;

            return cm;

        }


       


        public void AddCoverage(Coverage coverage)
        {
 
        }

        public void RemoveCoverage(Coverage coverage)
        {
 
        }


        public void AddConceptualModel(ConceptualModel model)
        {


            model.ParentMapData = this;
            this._conceptualModelCL.Add(model);
           

            if (this.ConceputalModelCollectionChanged != null)
            {
                this.ConceputalModelCollectionChanged(this, new CollecionChangedEventArgs(model, CollectionChangedType.ADD));
            }
 
        }

     




        public void RemoveConceputalModel(ConceptualModel model)
        {
            this._conceptualModelCL.Remove(model);
            model.ParentMapData = null;

           

            if (this.ConceputalModelCollectionChanged != null)
            {
                this.ConceputalModelCollectionChanged(this, new CollecionChangedEventArgs(model, CollectionChangedType.REMOVE));
            }

        }


        public ConceptualModel GetConcetualModel(string id)
        {
            foreach (ConceptualModel item in this._conceptualModelCL)
            {
                if (item.Key.ToString()==id)
                {
                    return item;
                    
                }
                
            }

            return null;
 
        }

        #endregion
    }
}
